#include <iostream>
#include <string>
#include <cstdlib>
#include <unistd.h>
#include <pthread.h>

using namespace std;
const int num = 5;

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

void *active(void *args){
    pthread_detach(pthread_self());
    string name = static_cast<const char *>(args);
    while(true){
        pthread_mutex_lock(&mutex);
        pthread_cond_wait(&cond, &mutex);
        cout << name << " active" << endl;
        pthread_mutex_unlock(&mutex);
    }
}

int main(){
    pthread_t tids[num];
    for(int i = 0; i < num; i++){
        char *name = new char[32];
        snprintf(name, 32, "thread-%d", i+1);
        pthread_create(tids+i, nullptr, active, name);
    }

    sleep(3);

    while(true){
        cout << "main thread wakeup thread" << endl;
        pthread_cond_signal(&cond);
        sleep(1);
    }

}